package cn.zhaoguang.usedcar.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zhaoguang.usedcar.common.page.PageRequest;
import cn.zhaoguang.usedcar.common.page.PageResult;
import cn.zhaoguang.usedcar.common.util.PageHelperUtil;
import cn.zhaoguang.usedcar.common.util.PasswordUtil;
import cn.zhaoguang.usedcar.common.util.RsaEncryptUtil;
import cn.zhaoguang.usedcar.main.entity.Seller;
import cn.zhaoguang.usedcar.main.entity.Usedcar;
import cn.zhaoguang.usedcar.main.entity.dto.SellerDTO;
import cn.zhaoguang.usedcar.main.entity.vo.SellerAddVO;
import cn.zhaoguang.usedcar.main.entity.vo.SellerUpdateInfoVO;
import cn.zhaoguang.usedcar.main.entity.vo.SellerUpdatePasswordVO;
import cn.zhaoguang.usedcar.main.mapper.SellerMapper;
import cn.zhaoguang.usedcar.main.service.SellerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/3 16:56
 */
@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerMapper sellerMapper;

    @Override
    public String add(SellerAddVO sellerAddVO) {
        Seller seller = sellerMapper.findSellerByPhone(sellerAddVO.getPhone());
        if (seller == null) {
            //获取RSA加密的密码并解密
            String rawPass = sellerAddVO.getPassword();
            String decodePassword = RsaEncryptUtil.privateKeyDecrypt(rawPass);

            //获取加密盐
            String salt = PasswordUtil.getSalt();
            //使用MD5加密密码用于存入数据库
            String password = PasswordUtil.encode(salt, decodePassword);

            Seller s = new Seller();
            s.setPhone(sellerAddVO.getPhone());
            s.setPassword(password);
            s.setEncryptSalt(salt);
            s.setCreateTime(DateUtil.date());
            int result = sellerMapper.insertSelective(s);
            if (result > 0) {
                return "添加商家成功";
            }
            return "添加商家失败";
        }
        return "添加失败! 手机号已存在";
    }

    @Override
    public int delete(Integer id) {
        return sellerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String updateInfo(SellerUpdateInfoVO sellerInfo) {
        Seller s = new Seller();
        BeanUtil.copyProperties(sellerInfo, s);

        int result = sellerMapper.updateByPrimaryKeySelective(s);
        if (result > 0) {
            return "商家信息修改成功";
        }
        return "商家信息修改失败";
    }

    @Override
    public String updatePassword(SellerUpdatePasswordVO sellerPassword) {
        //获取商家id
        Integer id = sellerPassword.getId();
        Seller s = sellerMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(s)) {
            //旧密码
            String oldPassword = sellerPassword.getOldPassword();
            //对比密码
            boolean result = PasswordUtil.matches(s.getEncryptSalt(), oldPassword, s.getPassword());
            if (result) {
                //获取新密码
                String NewPassword = sellerPassword.getNewPassword();
                //RSA解密
                String decodePassword = RsaEncryptUtil.privateKeyDecrypt(NewPassword);
                //获取加密盐
                String salt = PasswordUtil.getSalt();
                //使用MD5加密密码后用于存入数据库
                String password = PasswordUtil.encode(salt, decodePassword);

                int res = sellerMapper.updatePassword(id, salt, password);
                if (res > 0) {
                    return "密码修改成功";
                }
                return "密码修改失败";
            }
            return "旧密码不正确";
        }
        return "商家不存在";
    }

    @Override
    public Seller findSellerById(Integer id) {
        return sellerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Seller findSellerByPhone(String phone) {
        return sellerMapper.findSellerByPhone(phone);
    }

    @Override
    public List<SellerDTO> listAllSellerOnMap() {
        return sellerMapper.listAllSellerOnMap();
    }


    @Override
    public PageResult listAllSellerForManager(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());

        //获取查询参数
        String shopName = Convert.toStr(pageRequest.getParamByKey("shopName"));
        List<SellerDTO> sellerList = sellerMapper.listAllSellerForManager(shopName);

        PageResult result = PageHelperUtil.pageResult(new PageInfo<>(sellerList));
        return result;
    }
}
